<template>
    <div class="">
        <Upload
            ref="upload"
            :show-upload-list="false"
            :default-file-list="file"
            :on-success="handleSuccess"
            type="drag"
            :action="fileUploadUrl"
            :data="{dir:'device'}"
        >
            <Button icon="ios-cloud-upload-outline">
                {{ $t('ledger.uploadFiles') }}
            </Button>
        </Upload>
        <div v-if="Array.isArray(attachmentDTOS)">
            <Tag v-for="(item,index) in attachmentDTOS" :key="index"
                 color="blue"
                 type="border"
                 :closable="true"
                 @on-close="removeFile(item)"
                 @click.native="handlePreView(item)">
                {{ item.fileName }}
            </Tag>
        </div>
    </div>
</template>

<script>
import util from "@/util/util";

export default {
    name: "uploadFile",
    data() {
        return {
            file: [],
            attachmentDTOS: [],
            userId: "",
            fileUploadUrl: ""
        }
    },
    mounted() {
        this.fileUploadUrl = `${util.getEnv()}/fileServer/upload`
        this.userId = localStorage.getItem('ziyunFactoryUserId')
    },
    methods: {
      setFiles(list){
        this.file  = JSON.parse(JSON.stringify(list)) || []
        this.attachmentDTOS = JSON.parse(JSON.stringify(list)) || []
      },
        handlePreView(item) {
            if (item) {
                window.open(this.util.filePathToUrl(item.filePath));
            }
        },
        removeFile(file) {
            for (let i = 0; i < this.file.length; i++) {
                if (this.file[i].fileUrl === file.fileUrl) {
                    this.file.splice(i, 1)
                    break;
                }
            }
            for (let i = 0; i < this.attachmentDTOS.length; i++) {
                if (this.attachmentDTOS[i].fileUrl === file.fileUrl) {
                    this.attachmentDTOS.splice(i, 1)
                    break;
                }
            }
            this.onChange()
        },
        handleSuccess(res) {
            if (res.code === 200) {
                const {fileName, fileUrl, filepath, suffix} = res.data;
                this.file.push({name: fileName, url: fileUrl})
                if (!Array.isArray(this.attachmentDTOS))
                    this.attachmentDTOS = []
                this.attachmentDTOS.push({
                  fileName,
                  fileUrl,
                  filePath: filepath,
                  suffix,
                  // createBy: localStorage.getItem('ziyunFactoryUserId')
                })

                this.onChange()
            }
        },
        onChange() {
            // const list = this.attachmentDTOS.filter(item => {
            //     if (localStorage.getItem('ziyunFactoryUserId') / 1 === item.createBy / 1) {
            //         return item;
            //     }
            // })
            this.$emit('change',  this.attachmentDTOS)
        }
    }
}
</script>

<style scoped lang="scss">
ul {
    margin: 20px 0;
}

li {
    position: relative;
    height: 30px;
    border-radius: 5px;
    cursor: pointer;
    padding: 0 10px;

    &:hover {
        background: #f5f7f9;
    }

    a {
        display: inline-block;
        max-width: 80%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        line-height: 30px;
    }
}

.delete {
    position: absolute;
    color: red;
    right: 10px;
    top: 5px;
    display: inline-block;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    //background: rgba(0,0,0,.5);
    line-height: 20px;
    text-align: center;
    font-size: 15px;
}
</style>
